import axios from 'axios'
import { message } from 'antd'
import { Storage } from '@/assets/js/storage';
import { TOKEN } from '@/utils/constant'
import history from '@/utils/history'
import md5 from "js-md5";
import { decrypt } from "@/utils";
/** 
 * 封装axios
 * 
*/
let baseUrl = process.env.REACT_APP_BASEURL;
axios.defaults.baseUrl = baseUrl
axios.defaults.withCredentials = true //跨域
//配置发送请求前的拦截器 可以设置token信息 
axios.interceptors.request.use(config => {
  let tmd = "2mFA9KIkHOYHzoGPWnLRjE9lcGRDbLnDYomp641dGpI=";
  let bbq = "1QWERdfbIU12Q2vb";
  // 这里的config包含每次请求的内容
  let token = Storage.get(TOKEN);
  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token;
  }
  config.headers['webSide'] = 1;
  config.params = {
  ...config.params,
  };
  return config;
}, error => {
  console.log(error)
  message.destroy()
  message.error('请求出错：' + error.message)
  return Promise.reject(error);
})

// 配置响应拦截器 
axios.interceptors.response.use(res => {
  try {
    if (res.data.code !== 0) {
      let index = res.data.message.indexOf("错误状态码");
      let msg = "";
      if (index !== -1) {
        msg = res.data.message.substring(0, index);
      } else {
        msg = res.data.message;
      }
      message.destroy()
      message.warning(msg)
      if(msg.indexOf("未登录") !== -1 || msg.indexOf("您的账号已被锁定，无法操作")!== -1) {
        delete axios.defaults.headers['Authorization']
        Storage.clear()
        history.push('/login')
      }
    }
  } catch (error) {
    message.destroy()
    message.warning('网络出错')
  }
  return Promise.resolve(res.data);
}, error => {
  try {
    if (error.response) {
      switch (error.response.status) {
        case 401:
        case 403:
          message.destroy()
          message.warning('登录状态已失效，请重新登录')
          setTimeout(() => {
            Storage.clear()
            delete axios.defaults.headers['Authorization']
            history.push('/login')
          }, 1500)
          return error;
        case 500:
          message.destroy()
          message.warning('服务器出错')
          return error;
        default:
          return error;
      }
    }
    return Promise.reject(error.response) // 返回接口返回的错误信息
  } catch (error) {
    Storage.clear()
    delete axios.defaults.headers['Authorization']
    history.push('/login')
    return Promise.reject(error.response) 
  }
})
// eslint-disable-next-line import/no-anonymous-default-export
export default {
  get(url, params = {}, config = {}) {
    return axios.get(baseUrl + url, { ...config, params });
  },
  post(url, params = {}, config = {}) {
    return axios.post(baseUrl + url, params, {
      ...config,
    });
  },
  delete(url, data = {}, config = {}) {
    return axios.delete(baseUrl + url, {data}, {
      ...config,
    });
  },
  put(url, params = {}, config = {}) {
    return axios.put(baseUrl + url, params, {
      ...config,
    });
  },
  setToken(token) {
    axios.defaults.headers["Authorization"] = "Bearer " + token;
  },
  setXToKen(xtoken) {
    axios.defaults.headers["xtoken"] = xtoken;
  },
}
